package com.itheima.controller;

import com.itheima.pojo.Category;
import com.itheima.pojo.Result;
import com.itheima.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {


    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     */
    @PostMapping
    public Result addCategory(@RequestBody @Validated(Category.Add.class) Category category) {

        categoryService.addCategory(category);
        return Result.success("添加成功");
    }

    /**
     * 文章分类列表
     */
    @GetMapping
    public Result categoryList() {

        List<Category> list = categoryService.findListByCreateUser();
        return Result.success(list);
    }

    /**
     * 更新文章分类
     */
    @PutMapping
    public Result updateCategory(@RequestBody @Validated(Category.Update.class) Category category) {

        categoryService.updateCategory(category);
        return Result.success("更新成功");
    }

    /**
     * 根据id获取分类信息
     */
    @GetMapping("/detail")
    public Result<Category> getCategoryById(@RequestParam Integer id) {

        Category category = categoryService.getById(id);
        return Result.success(category);
    }

    /**
     * 删除分类
     */
    @DeleteMapping
    public Result deleteCategory(@RequestParam Integer id) {

        categoryService.deleteCategory(id);
        return Result.success("删除成功");
    }
}
